package com.zllkj.mm.base;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.zllkj.mm.module.UIHelper;
import com.zllkj.mm.rx.FragmentEvent;
import com.zllkj.mm.widgets.LoginLoadingDialog;

import androidx.annotation.Nullable;
import rx.Subscription;
import rx.subjects.PublishSubject;
import rx.subscriptions.CompositeSubscription;


public abstract class BaseAppFragment extends BaseFragment {

    public final PublishSubject<FragmentEvent> lifecycleSubject = PublishSubject.create();


    public LoginLoadingDialog loginLoadingDialog;

    // rx总线
    private CompositeSubscription compositeSubscription;

    public void addSubscribe(Subscription subscription) {
        if (compositeSubscription == null) {
            compositeSubscription = new CompositeSubscription();
        }
        compositeSubscription.add(subscription);
    }

    public void unSubscribe() {
        if (compositeSubscription != null) {
            compositeSubscription.unsubscribe();
            compositeSubscription = null;
        }
    }


    /**
     * 创建Dialog对象
     *
     * @param msg
     * @param isblack
     */
    public void startProgressDialog(String msg, boolean isblack) {
        loginLoadingDialog = new LoginLoadingDialog(getActivity(), msg, isblack);
        loginLoadingDialog.show();
    }

    public void startProgressDialog(boolean canCancel) {
        loginLoadingDialog = new LoginLoadingDialog(getActivity(),
                "玩命加载中", canCancel);
        loginLoadingDialog.show();
    }

    /**
     * 关闭Dialog对象
     */
    public void stopProgressDialog() {
        if (loginLoadingDialog != null) {
            loginLoadingDialog.close();
        }
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        lifecycleSubject.onNext(FragmentEvent.CREATE);
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        lifecycleSubject.onNext(FragmentEvent.CREATE_VIEW);
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onDestroyView() {
        lifecycleSubject.onNext(FragmentEvent.DESTROY_VIEW);
        super.onDestroyView();
        unSubscribe();
    }

    @Override
    public void onDestroy() {
        lifecycleSubject.onNext(FragmentEvent.DESTROY);
        super.onDestroy();
    }

//    //添加fragment
//    protected void addFragment(BaseFragment fragment) {
//        if (null != fragment && getHoldingActivity() instanceof BaseCommonActivity) {
//            ((BaseCommonActivity)getHoldingActivity()).addFragment(fragment);
//        }
//    }

    //添加fragment
    public void addFragment(BaseFragment fragment) {
        if (/*MainActivity.mainActivityResume*/true) {
            UIHelper.showPage(fragment);
            return;
        }
        if (null != fragment && getHoldingActivity() instanceof BaseCommonActivity) {
            ((BaseCommonActivity) getHoldingActivity()).addFragment(fragment);
        }
    }

    //移除fragment
    protected void removeFragment() {
        if (getHoldingActivity() instanceof BaseCommonActivity) {
            ((BaseCommonActivity) getHoldingActivity()).removeFragment();
        }
    }

}








